#include "EyerWASMBuffer.hpp"

namespace Eyer
{
    EyerWASMBuffer::EyerWASMBuffer()
    {
        Clear();
    }

    EyerWASMBuffer::~EyerWASMBuffer()
    {
        Clear();
    }

    int EyerWASMBuffer::Clear()
    {
        if(m_buf != nullptr){
            free(m_buf);
            m_buf = nullptr;
        }
        m_bufSize = 0;
        return 0;
    }

    EyerWASMBuffer::EyerWASMBuffer(const EyerWASMBuffer & buffer)
    {
        *this = buffer;
    }

    const EyerWASMBuffer & EyerWASMBuffer::operator = (const EyerWASMBuffer & buffer)
    {
        Clear();
        m_bufSize = buffer.m_bufSize;
        m_buf = (uint8_t *)malloc(m_bufSize);
        memcpy(m_buf, buffer.m_buf, m_bufSize);
        return *this;
    }

    int EyerWASMBuffer::SetBuffer(uint8_t * buf, size_t bufSize)
    {
        Clear();
        m_bufSize = bufSize;
        m_buf = (uint8_t *)malloc(m_bufSize);
        memcpy(m_buf, buf, m_bufSize);
        return 0;
    }

    uint8_t * EyerWASMBuffer::GetPtr()
    {
        return m_buf;
    }

    size_t EyerWASMBuffer::GetBufferSize()
    {
        return m_bufSize;
    }
}